﻿using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace APACWP8
{
    public class MarginFrameRoot : ContentControl
    {
        private readonly PhoneApplicationFrame frame;
        public bool IsDisplayed { get; set; }
        public MarginFrameRoot(PhoneApplicationFrame frame)
        {
            if (frame == null)
                throw new ArgumentNullException("frame");

            this.frame = frame;
            this.frame.OrientationChanged += frame_OrientationChanged;
        }

        void frame_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            var page = frame.Content as PhoneApplicationPage;
            var content = page.FindName("LayoutRoot") as Grid;
            if (content != null)
            {
                OrientationPage(e, content);
            }

        }

        private void OrientationPage(OrientationChangedEventArgs e, Grid content)
        {
            if ((e.Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait))
            {
                content.Margin = new Thickness(0, 0, 0, 0);
            }
            else if ((e.Orientation & PageOrientation.LandscapeLeft) == (PageOrientation.LandscapeLeft))
            {
                content.Margin = new Thickness(0, 0, 36, 0);
            }
            else if ((e.Orientation & PageOrientation.LandscapeRight) == (PageOrientation.LandscapeRight))
            {
                content.Margin = new Thickness(36, 0, 0, 0);
            }
          this.frame.UpdateLayout();
        }
    }
}
